package org.yzh.protocol.t808;

import io.github.yezhihao.protostar.ProtostarUtil;
import io.github.yezhihao.protostar.annotation.Field;
import io.github.yezhihao.protostar.annotation.Message;
import org.yzh.protocol.basics.JTMessage;
import org.yzh.protocol.commons.JT808;
import org.yzh.protocol.commons.transform.AttributeConverterXY;
import org.yzh.protocol.t808.utils.T0002MasterElecUtil;

import java.util.Map;

@Message(JT808.终端心跳)
public class T0002 extends JTMessage {

    @Field(length = 1, desc = "内置电池电量百分比")
    private int electric;

    @Field(length = 2, desc = "主电源电压和状态")
    private int masterElec;

    @Field(length = 1, desc = "无线网络信号强度")
    private int csq;

    @Field(length = 1, desc = "状态")
    private int state;

    @Field(converter = AttributeConverterXY.class, desc = "纽扣心跳附加信息", version = 0)
    private Map<Integer, Object> attributes;

    /**
     * 主电源电压
     */
    private double ev;

    /**
     * 充电状态 0-已插入充电 1-未插入充电
     */
    private int estate;

    public int getElectric() {
        return electric;
    }

    public void setElectric(int electric) {
        this.electric = electric;
    }

    public int getMasterElec() {
        return masterElec;
    }

    public void setMasterElec(int masterElec) {
        this.masterElec = masterElec;
        T0002MasterElecUtil.getMasterElectric(this);
    }

    public int getCsq() {
        return csq;
    }

    public void setCsq(int csq) {
        this.csq = csq;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public Map<Integer, Object> getAttributes() {
        return attributes;
    }

    public void setAttributes(Map<Integer, Object> attributes) {
        this.attributes = attributes;
    }

    public int getAttributeInt(int key) {
        if (attributes != null) {
            Integer value = (Integer) attributes.get(key);
            if (value != null) {
                return value;
            }
        }
        return 0;
    }

    public long getAttributeLong(int key) {
        if (attributes != null) {
            Long value = (Long) attributes.get(key);
            if (value != null) {
                return value;
            }
        }
        return 0L;
    }

    public double getEv() {
        return ev;
    }

    public void setEv(double ev) {
        this.ev = ev;
    }

    public int getEstate() {
        return estate;
    }

    public void setEstate(int estate) {
        this.estate = estate;
    }
}
